from dao.job_classes_dao import JobClassesDao
from sqlalchemy import distinct, desc, asc
from model.job_classes import JobClasses
class JobClassesService:
    def __init__(self, session):
        self.session = session

    def get_classes(self):
        dao = JobClassesDao(self.session)
        return dao.get_classes()

    def get_all_posts(self):
        dao = JobClassesDao(self.session)
        return dao.get_all_posts()

    def get_classes_dict(self):
        dicts = {}
        q = self.session.query(distinct(JobClasses.class_code).label("class_code"),
                               JobClasses.class_name)\
            .order_by(desc(JobClasses.class_code))
        result = q.all()
        if result:
            for d in result:
                dicts[d.class_code]=d.class_name
            return dicts
        else:
            return None

if __name__ == '__main__':
    from utils.work_db import Session
    session = Session()
    jser= JobClassesService(session)
    dd = jser.get_classes_dict()
    print(dd)
    session.close()